﻿Public Class EditHanhKhach
    Private bus_ThemHK As New BUS.EditHanhKhach
    Private dto_ThemHK As New DTO.EditHanhKhach
    Private bustracuuhk As New BUS.TraCuuKH
    Private dtotracuuhk As New DTO.TraCuuHK
    Private mahkedit As Integer
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Luu()
    End Sub
    
    Private Sub Luu()
        Dim n As Integer = 0
        If txtcmnd.Text = "" Or Not Long.TryParse(txtcmnd.Text, dto_ThemHK.cmnd) Then
            MessageBox.Show("Chứng minh thư nhân dân khách hàng không đúng!", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
            Exit Sub
        End If

        dto_ThemHK.hoten = txthoten.Text
        dto_ThemHK.mahk = mahkedit
        If dto_ThemHK.hoten = "" Then
            MessageBox.Show("Họ tên khách hàng không đúng!", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
            Exit Sub
        End If
        If txtdienthoai.Text = "" Or Not Long.TryParse(txtdienthoai.Text, dto_ThemHK.dienthoai) Then
            MessageBox.Show("Số điện thoại khách hàng không đúng!", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
            Exit Sub
        End If
        If bus_ThemHK.CheckHK(txtcmnd.Text) = True Then
            MessageBox.Show("Chứng minh nhân dân không tồn tại!", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
            Exit Sub
        End If
        Dim chek As Integer = bus_ThemHK.save(dto_ThemHK)
        If chek = Nothing Then
            MessageBox.Show("Không thể sửa. Xin bạn kiểm tra lại ràng buộc từ các bảng dữ liệu khác", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop)
            Exit Sub
        End If
        MessageBox.Show("Thành công!", "Thông báo", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation)
        Me.Hide()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Hide()
    End Sub

    Private Sub clall()
        txtcmnd.Text = ""
        txtdienthoai.Text = ""
        txthoten.Text = ""
    End Sub

    Private Sub ThemHanhKhach_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Hide()
        e.Cancel = True
        If Not Main.f_tracuuhk Is Nothing Then
            Main.f_tracuuhk.dgkhachhang.DataSource = bustracuuhk.dtload().Tables(0)
        End If
    End Sub

    Private Sub loaddt(ByVal mahkedit As Integer)
        Me.mahkedit = Val(Label5.Text)
        Dim dshk As DataSet = bus_ThemHK.dtload(Me.mahkedit)
        txtcmnd.Text = dshk.Tables(0).Rows(0).Item(3).ToString
        txtdienthoai.Text = dshk.Tables(0).Rows(0).Item(2).ToString
        txthoten.Text = dshk.Tables(0).Rows(0).Item(1).ToString

    End Sub

    Private Sub EditHanhKhach_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        loaddt(mahkedit)
    End Sub

    Private Sub EditHanhKhach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class